package com.atguigu.gulimall.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HelloController {

    @Value("${sso.server.url}")
    String url;

    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    @GetMapping("/employees")
    public String list(Model model, HttpSession session, @RequestParam(value = "token",required = false)String token) {
        // 通过 token 判断是否已经登录，不可以使用 springSession ，springSession只允许在同一域名下使用
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser == null && StringUtils.isEmpty(token)) {  // 此处略写判断逻辑
            System.out.println("url: redirect:"+url+"?redirect_url=client1.com:8081/list");
            return "redirect:"+url+"?redirect_url=client1.com:8081/employees";
        }
        List<String> list = new ArrayList<>();
        list.add("张三");
        list.add("李四");
        model.addAttribute("emps", list);
        return "list";
    }
}
